package pens.penObjects
{
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	
	import pens.PenType;

	/**
	 * 毛笔基类 
	 * @author Administrator
	 * 
	 */	
	public class Pen_Brush extends ActionBitmap
	{
	 
		private static var instance:Pen_Brush;
		/**
		 *单例模式
		 *
		 **/
		public static function getInstance():Pen_Brush{
		//	if(!instance) instance=new Pen_Brush();
			//return instance;
			
			return instance=!instance?new Pen_Brush():instance
		}
		/***************************************************/
		
		
		
		public static function dispose():void{
			instance=null
		}
		
		protected var oldX:Number;
		protected var oldY:Number;
		
		
		protected var oldScale:Number;

		protected var defaultScale:Number = .8;//默认笔触的大小
		protected var cx:Number = .02;//粗细变化参数
		protected var brushMin:Number = .1;//最细笔触限制
		
		protected var brushAlpha:Number = .5;//笔刷浓度
		
		
		
		protected var brushBlur:Number = 8;//笔刷模糊
		protected var midu:Number = .7;//笔刷密度
		
	//	protected var bf:BlurFilter;
		
		protected var nowScale:Number;
		
		private var br:BrushBit=new BrushBit()
		
		public function Pen_Brush()
		{
			
				
			super();
			_penName=PenType.PEN_BRUSH
				
				smoothing=true
			
		}
		
		override protected function _end():void
		{
			super._end();
		}
		
		override protected function _lineTo(x:Number, y:Number, size:Number=10, color:uint=0):void
		{
			//计算距离
			var _scale:Number = NaN;
			var disX:Number=x-oldX;
			var disY:Number=y-oldY;
			var dis:Number = Math.sqrt(disX * disX + disY * disY);			
			//改变笔触的大小,越快越小
			if (dis > 0) {
				_scale= defaultScale - dis * cx;
				if (_scale > 1) {
					_scale = 1;
				}else if (_scale < brushMin) {
					_scale = brushMin;
				}
				_scale = (oldScale + _scale) / 2*size/50;
				
			}
			//填充
			var count:* = Math.floor(dis / midu);
			var scaleBili:* = (oldScale-_scale) / count;
			var i:int = 0;
			while(i<(count - 1)){
				var scaY:Number = oldScale -i * scaleBili;	
				var scaX:Number=oldScale - i* scaleBili
				var matrix:Matrix=new Matrix()
				
				matrix.scale(scaX,scaY)
				matrix.tx=(disX/count)*(i+1)+oldX-br.width*.5*scaX
				matrix.ty=(disY/count)*(i+1)+oldY-br.height*.5*scaY
				var col:ColorTransform=new ColorTransform()					
				col.color=color
				col.alphaMultiplier=brushAlpha;
				bitmapData.draw(br,matrix,col,null,null,true)
				i++
			}
			oldScale = _scale;
			oldX = x;
			oldY = y;
		}
		
		override protected function _moveTo(x:Number, y:Number, size:Number=10, color:uint=0):void
		{
			oldX= x;
			oldY = y;
			oldScale = size/50
			
		}
	}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Matrix;


class BrushBit extends Bitmap{
	[Embed(source="../assets/brush.png")]
	private var _penPng:Class;
	private var _bit:Bitmap=new _penPng()
	public function BrushBit(){
		//_bit.x=-_bit.width*.5
	//	_bit.y=-_bit.height*.5
		
			var matrix:Matrix=new Matrix()
		//	matrix.tx=-_bit.width*.5
		//	matrix.ty=-_bit.height*.5
			this.bitmapData=new BitmapData(_bit.width,_bit.height,true,0xffffff)
			this.bitmapData.draw(_bit,matrix,null,null,null,true)
		
		//draw(_bit.width,_bit.height)
		_bit=null
	}
	
	private function draw(w:Number,h:Number):void{
	//	graphics.clear()
	//	var ma:Matrix=new Matrix(1,0,0,1,-w*.5,-h*.5)
		//ma.rotate(30);
	//	graphics.beginBitmapFill(_bit.bitmapData,ma,false,false)
		//graphics.lineStyle(_w)
		//	graphics.lineBitmapStyle(_bitm,ma,false,false)
	//	graphics.drawRect(-w*.5,-h*.5,w,h)
		//	graphics.lineTo(x,y)
	//	graphics.endFill()
	}
}